package flc.ast.fragment;

import android.app.Dialog;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import flc.ast.activity.AlbumActivity;
import flc.ast.R;
import flc.ast.adapter.AlbumAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.dialog.ChangeCoverDialog;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private List<MyAlbumBean> listShow = new ArrayList<>();
    private AlbumAdapter albumAdapter;
    private String albumId = "";
    private List<String> listSel = new ArrayList<>();
    private boolean isSingle;
    private boolean isEdit = false;
    private Dialog myAddAlbumDialog;
    private EditText dialogAdd;
    private Dialog myManageAlbumDialog;
    private ChangeCoverDialog myChangeCoverDialog;
    private Dialog myRenameAlbumDialog;
    private TextView dialogRename;
    private Dialog myDeleteAlbumDialog;


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mDataBinding.ivAlbumAdd.setOnClickListener(this);
        mDataBinding.tvAlbumAdd.setOnClickListener(this);
        mDataBinding.ivAlbumManage.setOnClickListener(this);
        mDataBinding.tvAlbumManageAllSel.setOnClickListener(this);
        mDataBinding.tvAlbumManageCancel.setOnClickListener(this);
        mDataBinding.ivAlbumDelete.setOnClickListener(this);

        mDataBinding.rvAlbumList.setLayoutManager(new GridLayoutManager(mContext, 3));
        albumAdapter = new AlbumAdapter();
        mDataBinding.rvAlbumList.setAdapter(albumAdapter);
        albumAdapter.setOnItemClickListener(this);
        albumAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                albumId = albumAdapter.getItem(position).getId();
                myManageAlbumDialog.show();
                return false;
            }
        });
        //管理相册弹框
        manageAlbumDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAlbumAdd://新建相册
            case R.id.tvAlbumAdd:
                //新建相册弹框
                addAlbumDialog();
                myAddAlbumDialog.show();
                break;
            case R.id.ivAlbumManage://相册管理
                isEdit = true;
                albumAdapter.setEdit(isEdit);
                mDataBinding.ivAlbumManage.setVisibility(View.GONE);
                mDataBinding.ivAlbumAdd.setVisibility(View.GONE);
                mDataBinding.tvAlbumManageAllSel.setVisibility(View.VISIBLE);
                mDataBinding.tvAlbumManageCancel.setVisibility(View.VISIBLE);
                mDataBinding.ivAlbumDelete.setVisibility(View.VISIBLE);
                break;
            case R.id.tvAlbumManageCancel://管理取消
                cancelEdit();
                break;
            case R.id.tvAlbumManageAllSel://全选
                listSel.clear();
                for (MyAlbumBean bean : listShow) {
                    bean.setSelected(true);
                    listSel.add(bean.getId());
                }
                albumAdapter.notifyDataSetChanged();
                break;
            case R.id.ivAlbumDelete://相册删除
                isSingle = false;
                deleteAlbumDialog();
                myDeleteAlbumDialog.show();
                break;
            //新建相册弹框
            case R.id.ivDialogAddAlbumConfirm://确定
                addAlbum();
                break;
            case R.id.ivDialogAddAlbumCancel://取消
                myAddAlbumDialog.dismiss();
                break;
            //管理相册弹框
            case R.id.tvDialogManageChangeCover://修改封面
                myManageAlbumDialog.dismiss();
                changeCoverDialog();
                break;
            case R.id.tvDialogManageRename://重命名
                myManageAlbumDialog.dismiss();
                renameAlbumDialog();
                myRenameAlbumDialog.show();
                break;
            case R.id.tvDialogManageDelete://删除
                myManageAlbumDialog.dismiss();
                isSingle = true;
                deleteAlbumDialog();
                myDeleteAlbumDialog.show();
                break;
            //重命名相册弹框
            case R.id.ivDialogRenameAlbumConfirm:
                rename();
                break;
            case R.id.ivDialogRenameAlbumCancel:
                myRenameAlbumDialog.dismiss();
                break;
            //删除相册弹框
            case R.id.ivDialogDeleteAlbumConfirm:
                if (isSingle) {
                    delete();
                } else {
                    deleteList();
                }
                myDeleteAlbumDialog.dismiss();
                break;
            case R.id.ivDialogDeleteAlbumCancel:
                myDeleteAlbumDialog.dismiss();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (albumAdapter.getItem(position).isSelected()) {
                albumAdapter.getItem(position).setSelected(false);
                listSel.remove(albumAdapter.getItem(position).getId());
            } else {
                albumAdapter.getItem(position).setSelected(true);
                listSel.add(albumAdapter.getItem(position).getId());
            }
            albumAdapter.notifyItemChanged(position);
        } else {
            AlbumActivity.albumId = albumAdapter.getItem(position).getId();
            startActivity(AlbumActivity.class);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            albumAdapter.setList(listShow);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.llAlbumNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.llAlbumNoData.setVisibility(View.VISIBLE);
        }
    }

    private void addAlbum() {
        String albumName = dialogAdd.getText().toString();
        if (TextUtils.isEmpty(albumName)) {
            ToastUtils.showShort(R.string.please_input_album_name);
            return;
        }
        myAddAlbumDialog.dismiss();
        //准备数据
        List<MyAlbumBean> listSave = new ArrayList<>();
        listSave.add(new MyAlbumBean(TimeUtils.getNowString(), albumName, "", new ArrayList<>(), false));
        //保存数据
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.savePicAlbumList(listSp);
        } else {
            AppSpUtils.savePicAlbumList(listSave);
        }
        getData();
    }

    private void changeCoverDialog() {
        if (myChangeCoverDialog == null) {
            myChangeCoverDialog = new ChangeCoverDialog(mContext,false);
        }
        myChangeCoverDialog.setListener(new ChangeCoverDialog.Listener() {
            @Override
            public void clickConfirm(String path) {
                changeCover(path);
            }
        });
        myChangeCoverDialog.show();
    }

    //修改封面
    private void changeCover(String path) {
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (int i = 0; i < listSp.size(); i++) {
                if (listSp.get(i).getId().equals(albumId)) {
                    listSp.get(i).setCover(path);
                    break;
                }
            }
            AppSpUtils.savePicAlbumList(listSp);
        }
        getData();
        ToastUtils.showShort(getString(R.string.change_suc));
    }

    //重命名
    private void rename() {
        String albumName = dialogRename.getText().toString();
        if (TextUtils.isEmpty(albumName)) {
            ToastUtils.showShort(R.string.please_input_album_name);
            return;
        }
        myRenameAlbumDialog.dismiss();
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (int i = 0; i < listSp.size(); i++) {
                if (listSp.get(i).getId().equals(albumId)) {
                    listSp.get(i).setName(albumName);
                    break;
                }
            }
            AppSpUtils.savePicAlbumList(listSp);
            ToastUtils.showShort(getString(R.string.save_success));
        }
        getData();
    }

    //删除相册
    private void delete() {
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (int i = 0; i < listSp.size(); i++) {
                if (listSp.get(i).getId().equals(albumId)) {
                    listSp.remove(i);
                    break;
                }
            }
            AppSpUtils.savePicAlbumList(listSp);
            ToastUtils.showShort(getString(R.string.delete_suc));
        }
        getData();
    }

    //多个删除相册
    private void deleteList() {
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            Iterator<MyAlbumBean> iterator = listSp.iterator();
            while (iterator.hasNext()) {
                MyAlbumBean item = iterator.next();
                for (String s : listSel) {
                    if (item.getId().equals(s)) {
                        iterator.remove();
                        break;
                    }
                }
            }
            AppSpUtils.savePicAlbumList(listSp);
            ToastUtils.showShort(getString(R.string.delete_suc));
        }
        cancelEdit();
    }

    private void cancelEdit() {
        isEdit = false;
        albumAdapter.setEdit(isEdit);
        getData();
        mDataBinding.ivAlbumManage.setVisibility(View.VISIBLE);
        mDataBinding.ivAlbumAdd.setVisibility(View.VISIBLE);
        mDataBinding.tvAlbumManageAllSel.setVisibility(View.GONE);
        mDataBinding.tvAlbumManageCancel.setVisibility(View.GONE);
        mDataBinding.ivAlbumDelete.setVisibility(View.GONE);
        listSel.clear();
    }

    //新建相册弹框
    private void addAlbumDialog() {
        myAddAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_album, null);
        myAddAlbumDialog.setContentView(inflate);
        myAddAlbumDialog.setCancelable(true);
        Window dialogWindow = myAddAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView right = inflate.findViewById(R.id.ivDialogAddAlbumConfirm);
        ImageView cancel = inflate.findViewById(R.id.ivDialogAddAlbumCancel);
        dialogAdd = inflate.findViewById(R.id.etDialogAddAlbumText);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    //管理相册弹框
    private void manageAlbumDialog() {
        myManageAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_manage_album, null);
        myManageAlbumDialog.setContentView(inflate);
        myManageAlbumDialog.setCancelable(true);
        Window dialogWindow = myManageAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView changeCover = inflate.findViewById(R.id.tvDialogManageChangeCover);
        TextView rename = inflate.findViewById(R.id.tvDialogManageRename);
        TextView delete = inflate.findViewById(R.id.tvDialogManageDelete);
        TextView cancel = inflate.findViewById(R.id.tvDialogManageCancel);
        changeCover.setOnClickListener(this);
        rename.setOnClickListener(this);
        delete.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    //重命名相册弹框
    private void renameAlbumDialog() {
        myRenameAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_rename_album, null);
        myRenameAlbumDialog.setContentView(inflate);
        myRenameAlbumDialog.setCancelable(true);
        Window dialogWindow = myRenameAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView right = inflate.findViewById(R.id.ivDialogRenameAlbumConfirm);
        ImageView cancel = inflate.findViewById(R.id.ivDialogRenameAlbumCancel);
        dialogRename = inflate.findViewById(R.id.etDialogRenameAlbumText);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
        List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
        if (listSp != null && listSp.size() > 0) {
            for (int i = 0; i < listSp.size(); i++) {
                if (listSp.get(i).getId().equals(albumId)) {
                    dialogRename.setText(listSp.get(i).getName());
                    break;
                }
            }
        }
    }

    //删除相册弹框
    private void deleteAlbumDialog() {
        myDeleteAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete_album, null);
        myDeleteAlbumDialog.setContentView(inflate);
        myDeleteAlbumDialog.setCancelable(true);
        Window dialogWindow = myDeleteAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView right = inflate.findViewById(R.id.ivDialogDeleteAlbumConfirm);
        ImageView cancel = inflate.findViewById(R.id.ivDialogDeleteAlbumCancel);
        TextView text = inflate.findViewById(R.id.tvDialogDeleteAlbumText);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
        String name = "";
        int size = 0;
        if (isSingle) {
            List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
            if (listSp != null && listSp.size() > 0) {
                for (int i = 0; i < listSp.size(); i++) {
                    if (listSp.get(i).getId().equals(albumId)) {
                        name = listSp.get(i).getName();
                        size = listSp.get(i).getListData().size();
                        break;
                    }
                }
                text.setText(getString(R.string.delete_text1, name, size));
            }
        } else {
            List<MyAlbumBean> listSp = AppSpUtils.getPicAlbumList();
            if (listSp != null && listSp.size() > 0) {
                Iterator<MyAlbumBean> iterator = listSp.iterator();
                while (iterator.hasNext()) {
                    MyAlbumBean item = iterator.next();
                    for (String s : listSel) {
                        if (item.getId().equals(s)) {
                            size += item.getListData().size();
                            break;
                        }
                    }
                }
                text.setText(getString(R.string.delete_text2, size));
            }
        }
    }


}